package com.wutianyi.task;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.ParserException;

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.wutianyi.dataobject.PostBean;
import com.wutianyi.html.HtmlParser;
import com.wutianyi.html.InfoHandle;
import com.wutianyi.html.impl.Ip138PostHandle;
import com.wutianyi.service.GeoApiServices;
import com.wutianyi.utils.HtmlUtils;
import com.wutianyi.utils.NetUtils;

import static com.wutianyi.entity.EntityTypeEnum.*;

public class Ip138PostTask implements Tasks {

	private InfoHandle<PostBean[]> handle = new Ip138PostHandle();

	@Override
	public void run() {

		try {
			// FIXME!
			String content = HtmlUtils.getBodyString(
					"http://www.ip138.com/post/shanghai/", "gb2312");
			HtmlParser parser = new HtmlParser(content);
			List<PostBean[]> postBeans = parser.parser(handle,
					new NodeFilter[] { new TagNameFilter("tr"),
							new HasAttributeFilter("bgcolor", "#ffffff") });
			// FIXME!
			String country = "中国";
			String province = "上海";
			String city = "上海市";
			Map<String, Entity> maps = GeoApiServices.getEntityByProvinces(
					country, province);
			Key cKey = KeyFactory.createKey(Country.name(), country);
			Key pKey = KeyFactory.createKey(cKey, Province.name(), province);
			Key cityKey = null;
			for (PostBean[] ps : postBeans) {
				if (ps.length == 1) {
					if (ps[0].getName().equals("辖区")
							|| ps[0].getName().equals("辖县")) {
						cityKey = KeyFactory.createKey(pKey, City.name(), city);
					} else {
						cityKey = KeyFactory.createKey(pKey, City.name(), ps[0]
								.getName());
					}

					Entity entity = maps.get(cityKey.toString());
					if (null == entity) {
						entity = new Entity(City.name(), ps[0].getName(), pKey);
						maps.put(cityKey.toString(), entity);
					}
					entity.setProperty("post", ps[0].getPost());
					entity.setProperty("zone", ps[0].getZone());
				} else {
					Key aKey = KeyFactory.createKey(cityKey, Area.name(), ps[0]
							.getName());
					Entity entity = maps.get(aKey.toString());
					if (null == entity) {
						entity = new Entity(Area.name(), ps[0].getName(), aKey);
						maps.put(aKey.toString(), entity);
					}
					entity.setProperty("post", ps[0].getPost());
					entity.setProperty("zone", ps[0].getZone());
					if (null != ps[1]) {
						aKey = KeyFactory.createKey(cityKey, Area.name(), ps[1]
								.getName());
						entity = maps.get(aKey.toString());
						if (null == entity) {
							entity = new Entity(Area.name(), ps[1].getName(),
									aKey);
							maps.put(aKey.toString(), entity);
						}
						entity.setProperty("post", ps[1].getPost());
						entity.setProperty("zone", ps[1].getZone());
					}
				}
			}
			DatastoreServiceFactory.getDatastoreService().put(maps.values());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void run(HttpServletRequest request) {
		try {
			String url = request.getParameter("url");
			String encode = NetUtils.getParameterString(request, "encode", "gb2312");
			String content = HtmlUtils.getBodyString(url, encode);
			HtmlParser parser = new HtmlParser(content);
			List<PostBean[]> postBeans = parser.parser(handle,
					new NodeFilter[] { new TagNameFilter("tr"),
							new HasAttributeFilter("bgcolor", "#ffffff") });
			// FIXME!
			String country = NetUtils.getParameterString(request, "country", "中国");
			String province = NetUtils.getParameterString(request, "province", "广东省");
			String city = NetUtils.getParameterString(request, "city", "广州市");
			Map<String, Entity> maps = GeoApiServices.getEntityByProvinces(
					country, province);
			Key cKey = KeyFactory.createKey(Country.name(), country);
			Key pKey = KeyFactory.createKey(cKey, Province.name(), province);
			Key cityKey = null;
			for (PostBean[] ps : postBeans) {
				if (ps.length == 1) {
					if (ps[0].getName().equals("辖区")
							|| ps[0].getName().equals("辖县")) {
						cityKey = KeyFactory.createKey(pKey, City.name(), city);
					} else {
						cityKey = KeyFactory.createKey(pKey, City.name(), ps[0]
								.getName());
					}

					Entity entity = maps.get(cityKey.toString());
					if (null == entity) {
						entity = new Entity(City.name(), ps[0].getName(), pKey);
						maps.put(cityKey.toString(), entity);
					}
					entity.setProperty("post", ps[0].getPost());
					entity.setProperty("zone", ps[0].getZone());
				} else {
					Key aKey = KeyFactory.createKey(cityKey, Area.name(), ps[0]
							.getName());
					Entity entity = maps.get(aKey.toString());
					if (null == entity) {
						entity = new Entity(Area.name(), ps[0].getName(), aKey);
						maps.put(aKey.toString(), entity);
					}
					entity.setProperty("post", ps[0].getPost());
					entity.setProperty("zone", ps[0].getZone());
					if (null != ps[1]) {
						aKey = KeyFactory.createKey(cityKey, Area.name(), ps[1]
								.getName());
						entity = maps.get(aKey.toString());
						if (null == entity) {
							entity = new Entity(Area.name(), ps[1].getName(),
									aKey);
							maps.put(aKey.toString(), entity);
						}
						entity.setProperty("post", ps[1].getPost());
						entity.setProperty("zone", ps[1].getZone());
					}
				}
			}
			DatastoreServiceFactory.getDatastoreService().put(maps.values());
			GeoApiServices.refreshAllGeoInformation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		String content = HtmlUtils.getBodyString(
				"http://www.ip138.com/post/guangdong/", "gb2312");
		System.out.println(content);
	}

}
